package com.yuexunit.operationman.nbsy.module.plant.presenter.impl;

import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.plant.bean.ImplantMaterialItemListEntity;
import com.yuexunit.operationman.nbsy.module.plant.bean.MaterialDeviceBean;
import com.yuexunit.operationman.nbsy.module.plant.model.impl.MaterialDeviceModel;
import com.yuexunit.operationman.nbsy.module.plant.presenter.IMaterialDevicePresenter;
import com.yuexunit.operationman.nbsy.module.plant.ui.view.IMaterialDeviceView;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by chenss on 2017/7/20 0020.
 */

public class MaterialDevicePresenter implements IMaterialDevicePresenter {

    private MaterialDeviceModel mModel;
    private IMaterialDeviceView mView;

    public MaterialDevicePresenter(IMaterialDeviceView view, MaterialDeviceModel model) {
        mModel = model;
        mView = view;
    }

    @Override
    public void getMaterialDevice(Map<String, String> praise) {
        mModel.getMaterialDevice(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mView.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MaterialDeviceBean<ImplantMaterialItemListEntity>>() {
                    @Override
                    public void onCompleted() {
                        mView.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                    }

                    @Override
                    public void onNext(MaterialDeviceBean<ImplantMaterialItemListEntity> materialDeviceBean) {
                        mView.getMaterialDevice(materialDeviceBean);
                    }
                });
    }
}
